package com.ktjiaoyu.news.servlet.admin;

import com.alibaba.fastjson.JSON;
import com.ktjiaoyu.news.entity.Goods;
import com.ktjiaoyu.news.service.GoodsSvrvice;
import com.ktjiaoyu.news.service.impl.GoodsServicelmpl;
import com.ktjiaoyu.news.utils.R;
import com.ktjiaoyu.news.utils.RequestUtil;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "GoodsServlet", urlPatterns = "/api/admin/goods")
public class GoodsServlet extends HttpServlet {
    GoodsSvrvice service = null;

    @Override
    public void init(ServletConfig config) throws ServletException {
        service = new GoodsServicelmpl();
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");

        String opr = request.getParameter("opr");
//        int id;  //商品id，自增长
//        int goodType;  //商品分类id
//        String mane;  //名称
//        int age;  //数量
//        double price; // 价格
//        int inventory;  //存货
//        int category;  //0.水果类1.肉类2.熟食区3.鱼类
//        String describe;  //描述
//        String address;  //商品地址
//        String listing;  //上货时间
//        String removal;  //下货时间
        Integer id = RequestUtil.parseInt(request, "id", null);
        Integer goodType = RequestUtil.parseInt(request, "goodType", null);
        String mane = request.getParameter("mane");
        // Date birthDate = RequestUtil.parseDate(request, "birthDate", null, "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss");
        Integer age = RequestUtil.parseInt(request, "age", null);
        Integer price = RequestUtil.parseInt(request, "price", null);
        Integer inventory = RequestUtil.parseInt(request, "inventory", null);
        Integer category = RequestUtil.parseInt(request, "category", null);
        String describe = request.getParameter("describe");
        String address = request.getParameter("address");
        String listing = request.getParameter("listing");
        String removal = request.getParameter("removal");
        Goods goods = new Goods();
        goods.setId(id);
        goods.setMane(mane);
        goods.setAge(age);
        goods.setPrice(price);
        goods.setInventory(inventory);
        goods.setCategory(category);
        goods.setDescribe(describe);
        goods.setAddress(address);
        goods.setListing(listing);
        goods.setRemoval(removal);
        goods.setGoodType(goodType);

        R result = null;
        if ("page".equals(opr)) {
            // 分页查询
            Integer pageNo = RequestUtil.parseInt(request, "pageNo", 1);
            Integer pageSize = RequestUtil.parseInt(request, "pageSize", 4);
            List<Goods> page = service.getuserInfoPage(goods.getMane(), pageNo, pageSize);
            // 返回分页数据
            result = R.ok(page);
        } else if ("list".equals(opr)) {
            // 列表全部数据
            List<Goods> list = service.getAllGoodsList();
            // 返回所有数据
            result = R.ok(list);
        } else if ("save".equals(opr)) {
            // 新增数据
            int count = service.insert(goods);
            if (count > 0) {
                result = R.ok(goods);
            } else {
                result = R.fail(goods);
            }
        } else if ("findById".equals(opr)) {
            // 获取详情数据
            goods = service.findById(id);
            if (goods != null) {
                result = R.ok(goods);
            } else {
                result = R.fail();
            }
        } else if ("update".equals(opr)) {
            // 修改数据
            int count = service.update(goods);
            if (count > 0) {
                result = R.ok(goods);
            } else {
                result = R.fail(goods);
            }
        } else if ("del".equals(opr)) {
            // 删除单条数据
            int deleted = service.delById(id);
            if (deleted > 0) {
                result = R.ok(id);
            } else {
                result = R.fail(id);
            }
        }
        PrintWriter out = response.getWriter();
        out.print(JSON.toJSONString(result));
        out.flush();
        out.close();
    }
}
